package com.wondersgroup.fjzlpt.org.dao.impl.jtqy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.quick.framework.boot.repository.sql.dialect.DialectProxy;
import com.quick.framework.util.FormatDate;
import com.wondersgroup.fjzlpt.org.util.ControllerCommonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.ZjsqDao;

@Repository
public class ZjsqDaoImpl extends BaseJTDAOImpl implements ZjsqDao{

	@Autowired
	private DialectProxy dialectProxy;

	@Override
	@Autowired
	@Qualifier("TB_JTQY_ZJSQ")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	/**
	 * 查询待审核的转介记录
	 * @param map
	 * @return
	 */
	public JSONArray queryAll(Map<String, String> map){
		JSONObject zjshObj = getZjshCondition(map);
		List<String> sqlParams = (List<String>)zjshObj.get("sqlParams");
		SQLBean bean = new SQLBean(zjshObj.getString("sql"), sqlParams.toArray());
		return super.queryList(bean);
	}
	
	/**
	 * 分页查询待审核的转介记录
	 * @param map
	 * @return
	 */
	@Override
	public JSONObject queryPage(Map<String, String> map) {
		JSONObject zjshObj = getZjshCondition(map);
		ControllerCommonUtil.pageFormat(map);
		List<String> sqlParams = (List<String>)zjshObj.get("sqlParams");
		//PageQuerySQLBean pageBean = new PageQuerySQLBean(zjshObj.get("sql").toString(), pageSize, pageNumber , " order by t.SQRQ desc ",sqlParams.toArray());
		PageQuerySQLBean pageBean = super.checkAndCreatePageQuerySQLBean(map, zjshObj.getString("sql"), null, sqlParams.toArray());
		return super.queryPage(pageBean, ResultHandlerConstant.JSONARRAY);
	}
	
	/**
	 * 生成转介审核参数
	 * @param map
	 * @return
	 */
	private JSONObject getZjshCondition(Map<String, String> map){
		String groupFunName = dialectProxy.getRow2ColumnFuncName(this.getJdbcTemp().getDataSource());
		JSONObject resultObj = new JSONObject();
		
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" SELECT * FROM v_jtqy_zjsq where 1 =1 ");

		
		List sqlParams = new ArrayList();
		//患者姓名
		String value = map.get("XM");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and XM like ?");
			sqlParams.add("%"+value+"%");
		}
		//证件号码
		value = map.get("ZJHM");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and ZJHM like ?");
			sqlParams.add(value + "%");
		}
		//医疗机构名称
		value = map.get("YLJGMC");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and YLJGMC like ?");
			sqlParams.add("%" + value + "%");
		}
		//医疗机构ID
		value = map.get("YLJGID");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and YLJGID = ?");
			sqlParams.add(value);
		}
		//现签约团队
		value = map.get("XQYTD");
		if(!StringUtils.isEmpty(value)){
			String[] xqytdAry = value.split(",");
			sqlBuf.append("and XQYTD in ( ");
			for (String xqytd : xqytdAry) {
				sqlBuf.append("?,");
				sqlParams.add(xqytd);
			}
			sqlBuf.deleteCharAt(sqlBuf.length()-1).append(" ) ");
			//sqlBuf.append(" and XQYTD in ?");
			//sqlParams.add(value);
		}
		//原签约团队
		value = map.get("YQYTD");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and YQYTD in ( ");
			String[] tdidArr = value.split(",");
			for (String tdid : tdidArr) {
				sqlBuf.append("?,");
				sqlParams.add(tdid);
			}
			sqlBuf.deleteCharAt(sqlBuf.length()-1).append(")");
		}
		//审核状态
		value = map.get("SHZT");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and SHZT = ?");
			sqlParams.add(value);
		}
		//签约时间段
		value = map.get("QYSJ_start");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and QYSJ >= ?");
			sqlParams.add(FormatDate.getDate("yyyy-MM-dd",value));
		}
		value = map.get("QYSJ_end");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and QYSJ <= ?");
			sqlParams.add(FormatDate.addDay(FormatDate.getDate("yyyy-MM-dd",value),1,"yyyy-MM-dd"));
		}
		//转介时间段
		value = map.get("SQRQ_start");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and SQRQ >= ?");
			sqlParams.add(FormatDate.getDate("yyyy-MM-dd",value));
		}
		value = map.get("SQRQ_end");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and SQRQ <= ?");
			sqlParams.add(FormatDate.addDay(FormatDate.getDate("yyyy-MM-dd",value),1,"yyyy-MM-dd"));
		}
		//服务包ID
		value = map.get("FWBID");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and FWBID like ?");
			sqlParams.add("%"+value+"%");
		}

		sqlBuf.append(" order by SQRQ desc");
		resultObj.put("sql", sqlBuf.toString());
		resultObj.put("sqlParams", sqlParams);
		return resultObj;
	}

	/**
	 * 转介申请记录服务包信息
	 * @param list
	 */
	@Override
	public void zjsqFwbAdd(List<Object[]> list) {
		SQLBean sqlb = new SQLBean();
		String sql = "insert into TB_JTQY_ZJFWB values (?,?) ";
		sqlb.setSql(sql);
		sqlb.setParamAry(list);
		sqlb.setBatch(true);
		super.update(sqlb);
	}
	
	
	
	
}
